<?php

	if(current_user_can('manage_options')){
	
	/*-----------------------------------------------------------------------------------*/
	/* Theme News Structure */
	/*-----------------------------------------------------------------------------------*/
	
	add_action('init', 'news_register');

	function news_register() {

		$labels = array(
			'name' => _x('News', 'post type general name'),
			'singular_name' => _x('News Item', 'post type singular name'),
			'add_new' => _x('Add New', 'news item'),
			'add_new_item' => __('Add New News Item'),
			'edit_item' => __('Edit News Item'),
			'new_item' => __('New News Item'),
			'view_item' => __('View News Item'),
			'search_items' => __('Search News'),
			'not_found' =>  __('Nothing found'),
			'not_found_in_trash' => __('Nothing found in Trash'),
			'parent_item_colon' => ''
		);

		$args = array(
			'labels' => $labels,
			'public' => true,
			'publicly_queryable' => true,
			'show_ui' => true,
			'query_var' => true,
			'menu_icon' => get_stylesheet_directory_uri() . '/img/newsicon.png',
			'rewrite' => true,
			'capability_type' => 'post',
			'hierarchical' => false,
			'menu_position' => null,
			'supports' => array('title','editor','thumbnail')
		  ); 

		register_post_type( 'news' , $args );
	}
	
	/*-----------------------------------------------------------------------------------*/
	/* Add Metabox to News Post */
	/*-----------------------------------------------------------------------------------*/

	//Add metabox to News template
	
	add_action('admin_menu', 'addMetaBoxN');
	add_action('save_post', 'saveMetaDataN', 10, 2);
	add_action('admin_head', 'embedUploaderCode');
	 
	//Define the metabox attributes.
	$metaBoxN = array(
	  'id'     => 'arc_newsimg_metabox',
	  'title'    => 'News Top Image',
	  'page'     => 'news',
	  'context'  => 'normal',
	  'priority'   => 'low',
	  'fields' => array(
	    array(
	      'name'   => 'Custom Image',
	      'desc'   => '<em>Recommended photo size: 395 x 110 px</em>',
	      'id'  => 'arc_news_image',  //value is stored with this as key.
	      'class' => 'image_upload_field',
	      'type'   => 'media'
	    )
	  )
	);
	
	function addMetaBoxN() {
	  global $metaBoxN;
	  add_meta_box($metaBoxN['id'], $metaBoxN['title'], 'createMetaBoxN', 
	    $metaBoxN['page'], $metaBoxN['context'], $metaBoxN['priority']);
	 
	}
	
	/**
	* Create Metabox HTML.
	*/
	function createMetaBoxN($post) {
	  global $metaBoxN;
	  if (function_exists('wp_nonce_field')) {
	    wp_nonce_field('awd_nonce_action','awd_nonce_field');
	  }
	 
	  foreach ($metaBoxN['fields'] as $field) {
	    echo '<div class="awdMetaBoxN">';
	    //get attachment id if it exists.
	    $meta = get_post_meta($post->ID, $field['id'], true);
	    switch ($field['type']) {
	      case 'media':
	?>
	        <p><?php echo $field['desc']; ?></p>
	        <div class="awdMetaImage">
	<?php 
	        if ($meta) {
	          echo wp_get_attachment_image( $meta, 'thumbnail', true);
	          $attachUrl = wp_get_attachment_url($meta);
	          echo '<p>URL: <a target="_blank" href="'.$attachUrl.'">'.$attachUrl.'</a></p>';
	        }
	?>    
	        </div><!-- end .awdMetaImage -->
	        <p>
	          <input type="hidden" 
	            class="metaValueField" 
	            id="<?php echo $field['id']; ?>" 
	            name="<?php echo $field['id']; ?>"
	            value="<?php echo $meta; ?>" 
	          /> 
	          <input class="image_upload_button"  type="button" value="Choose File" /> 
	          <input class="removeImageBtn" type="button" value="Remove File" />
	        </p>
	 
	<?php
	      break;
	    }
	    echo '</div> <!-- end .awdMetaBox -->';
	  } //end foreach
	}//end function createMetaBox
	 	 
	function saveMetaDataN($post_id, $post) {
	  //make sure we're saving at the right time.
	  //DOING_AJAX is set when saving a quick edit on the page that displays all posts/pages  
	  //Not checking for this will cause our meta data to be overwritten with blank data.
	  if ( empty($_POST)
	    || !wp_verify_nonce($_POST['awd_nonce_field'],'awd_nonce_action')
	    || $post->post_type == 'revision'
	    || defined('DOING_AJAX' )) {
	    return;
	  }
	 
	  global $metaBoxN;
	  global $wpdb;
	 
	  foreach ($metaBoxN['fields'] as $field) {
	    $value = $_POST[$field['id']];
	 
	    if ($field['type'] == 'media' && !is_numeric($value) ) {
	      //Convert URL to Attachment ID.
	      $value = $wpdb->get_var(
	        "SELECT ID FROM $wpdb->posts 
	         WHERE guid = '$value' 
	         AND post_type='attachment' LIMIT 1");
	    }
	    update_post_meta($post_id, $field['id'], $value);
	  }//end foreach
	}//end function saveMetaDataN
	
}
?>